package cn.wolfcode.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageBean;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int pageSize,
                       String name,
                       Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date end
    ) {
        PageBean pageBean = empService.query(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }
  @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Long> ids) {
        empService.batchDelete(ids);
        return Result.success();
    }
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        empService.save(emp);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selById(@PathVariable("id") Long id) {
        Emp emp = empService.selById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }
}
